//
// FileDocument.cs
//  Gtk Window
//
// Author: 
//  Fredy Ramirez <framirez@pacificcoolwave.com>
//
// Copyright (C) 2011 Fredy Ramirez (http://www.pacificcoolwave.com)
//
//  Creation date: Sep 16, 2011
//  Last modification: 

/*
The MIT License

Copyright (C) 2011 Fredy Ramirez (http://www.pacificcoolwave.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */


using System;
using System.Collections.Generic;
using System.IO;
using Gtk;

namespace Pcw.PcwTextEditor.PcwDocument
{
	public class FileDocument : IDocumentType
	{
		private string ext;
		private string name;
		private string path;

		private bool is_new;
		private bool saved;
		
		private Dictionary<string, object> data = new Dictionary<string, object>();
		
		private TextBuffer buffer;
		
		public event DocumentTypeEventHandler DocumentTypeCreate;
		public event DocumentTypeEventHandler DocumentTypeClose;
		public event DocumentTypeEventHandler DocumentTypeSave;
		public event DocumentTypeEventHandler DocumentTypeLoad;
		
		public bool IsNew {
			get  {
				return is_new;
			}
		}

		public bool Saved {
			get  {
				return saved;
			}
			set{
				saved = value;
			}
		}

		public string Name {
			get {
				return name;
			}
		}		
		
		public string Extension {
			get {
				return ext;
			}
		}
		
		public string NameWithExtension {
			get {
				if (!String.IsNullOrEmpty(ext))
					return name + "." + ext;
				else
					return name;
			}
		}
		
		public string Path {
			get {
				return path;
			}
		}

		public string NameWithPath {
			get {
				return path + NameWithExtension;
			}
			set {
				ResolveName(value);
			}
		}
		
		public TextBuffer Buffer {
			set {
				buffer = value;
			}
		}
		
		public string Text {
			get {
				return buffer.Text;
			}
		}
		
		public Dictionary<string, object> Data {
			get {
				return data;
			}
		}		
		
		public FileDocument (string file)
		{
			ResolveName(file);
		}
		
		public bool Open() 
		{			
			if (File.Exists(NameWithPath))
				buffer.Text = File.ReadAllText(NameWithPath);
			else
			    buffer.Text = "";
			
			if(is_new) 
				if (DocumentTypeCreate != null)
					DocumentTypeCreate(this);
			
			if (DocumentTypeLoad != null)
				DocumentTypeLoad(this);
			
			return true;
		}
		
		public bool Close() 
		{
			if (DocumentTypeClose != null)
				DocumentTypeClose(this);
			return true;
		}
		
		public bool Save()
		{
			File.WriteAllText (NameWithPath, buffer.Text);
			saved = true;
			
			if(DocumentTypeSave != null)
				DocumentTypeSave(this);
			
			return true;
		}
		
		private void ResolveName(string file) 
		{
			if (!String.IsNullOrEmpty(file)) {
				string separator = Convert.ToString (System.IO.Path.DirectorySeparatorChar);
				int ls = file.LastIndexOf(separator);			
				
				name = file.Substring(ls + 1);
				path = file.Substring(0, ls + 1);
				
				ls = name.LastIndexOf(".");
				ext = name.Substring(ls + 1);
				name = name.Substring(0, ls);
				
				is_new = false;
			
			}
			else {				
				name = Document.NextNewFileName();
				is_new = true;
			}
		}
	}
}

